/*******************************************************************************
 * Copyright (C) 2007, Google Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package com.google.eclipse.mechanic.plugin.ui;

import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;

import alpha.com.google.code.mechanic.janitor.ui.PrefRecorderOutputWizard;

import com.google.eclipse.mechanic.core.recorder.ChangeCollector;
import com.google.eclipse.mechanic.core.recorder.IPreferenceRecordingService;
import com.google.eclipse.mechanic.plugin.core.MechanicLog;
import com.google.eclipse.mechanic.plugin.core.MechanicPlugin;

/**
 * Action that opens the a preferences dialog given a preferences page id.
 * 
 * <p>Do something like this:
 * <pre>
 * // create a new instance of the action
 * private static final Action openPrefsAction
 *    = new OpenPreferencesAction(
 *        OpenPreferencesAction.getPreferencesPageId(MyPlugin.PLUGIN_ID));
 * </pre>
 * 
 * <p>Then add the action to a context menu using:
 * {@code IContributionManager#add(IAction)}. FYI, {@code IMenuManager} extends
 * {@code IContributionManager}.
 * 
 * @author smckay@google.com (Steve McKay)
 */
public final class OpenPreferencesAction extends Action {

  // the default text to be displayed for the menu item
  private static final String DEFAULT_TEXT = "Preferences...";

  // id of the preferences page
  private final String pageId;
  
  // To start recording preferences changes before opening the Preferences Dialog.
  // Added by Jingjie Jiang
  private final IPreferenceRecordingService preferenceRecordingService;

  /**
   * Creates a new instance using the supplied page id and
   * label text.
   */
  public OpenPreferencesAction(String pageId, String text) {
    
    // Added by Jingjie Jiang
    this.preferenceRecordingService = MechanicPlugin.getDefault().getPreferenceRecordingService();
   
    this.pageId = pageId;
    setText(text);
  }

  /**
   * Creates a new instance using the supplied page id, supplying
   * default "Preferences..." label text.
   */
  public OpenPreferencesAction(String pageId) {
    
    this(pageId, DEFAULT_TEXT);
  }

  @Override
  public void run() {
    openPreferences();
  }

  /**
   * Opens the preferences dialog.
   */
  private void openPreferences() {
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
        shell, pageId, null, null);

    // Added by Jingjie Jiang
    startRecording();
    
    dialog.open();
    
    // Added by Jingjie Jiang
    stopRecording();

    // not sure if I have to do any cleanup. If so, the we probably want to:
    // dialog.blockOnOpen(true), then do the cleanup

    // not sure if I have to do any cleanup. If so, the we probably want to:
    // dialog.blockOnOpen(true), then do the cleanup
  }
  
  /**
   * To start Preference Recorder.
   * @author Jingjie Jiang
   */
  private void startRecording() {
    
    try {
      preferenceRecordingService.startRecording();
    } catch (CoreException e) {
      MechanicPlugin.getDefault().getLog().log(e.getStatus());
    }
  }
  
  /**
   * To stop Preference Recorder.
   * @author Jingjie Jiang
   */
  private void stopRecording() {
    try {
      // Stop recording, and collect results
      ChangeCollector collector = new ChangeCollector();
      preferenceRecordingService.endRecording(collector);

      // Display dialog to get obtain properties of the saved task file
      Shell parentShell = Display.getCurrent().getActiveShell();
      Map<String, String> preferences = collector.getPreferences();
      if (preferences.isEmpty()) {
        MessageDialog.openWarning(parentShell,  "No preferences",
            "No changes to preferences were made.");
      } else {
        // EpfOutputDialog dialog = new EpfOutputDialog(parentShell,  preferences);
        // added by jingjie jiang, Oct 5, 2014
        WizardDialog dialog =
            new WizardDialog (parentShell, new PrefRecorderOutputWizard(preferences));
        int dialogopen = dialog.open();
        if (Window.CANCEL == dialogopen) {
          // TODO: add handling function
          System.out.println("******** hello world **********");
        }
        else {
          // TODO: add handling function
          // if click the "Finish" button.
          int a = 0;
        }
        
      }
    } catch (CoreException e) {
      MechanicLog.getDefault().logError(e);
    }
  }
}
